package cn.jsl.basic.controller;

import cn.jsl.basic.dto.SmsDto;
import cn.jsl.basic.exception.MyException;
import cn.jsl.basic.service.IVerifyCodeService;
import cn.jsl.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 接口类：验证码
 * 1.图形验证码
 * 2.手机验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败!");
        }
    }

    /**
     * 获取手机验证码
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.sendSmsCode(smsDto);
            return new AjaxResult();
        } catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙!!!");
        }
    }

    /**
     * 获取手机验证码
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsDto smsDto){
        try {
            verifyCodeService.binderSmsCode(smsDto);
            return new AjaxResult();
        } catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,"发送失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙!!!");
        }
    }
}
